package edu.mtu.citizenscience.streamapplication.activity.main;

import edu.mtu.citizenscience.streamapplication.activity.DBHelper;
import edu.mtu.citizenscience.streamapplication.activity.HTMLView;
import edu.mtu.citizenscience.streamapplication.activity.R;
import edu.mtu.citizenscience.streamapplication.activity.R.layout;
import edu.mtu.citizenscience.streamapplication.activity.R.menu;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Adapter;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

public class SCStreamActivity extends Activity {
	
	
	// elements
	private EditText upWidth;
	private EditText downWidth;
	private EditText upDepth;
	private EditText downDepth;
	private Spinner leftAveCurrent;
	private Spinner rightAveCurrent;
	private Spinner leftSubstrate;
	private Spinner rightSubstrate;
	private RadioGroup streamRadioGroup;
	private EditText streamComments;
	
	private TextView header; // %^&*
	
	// Data base variables
	private DBHelper myDbHelper;
	private Long rowID;
	public static final String ROW_ID = "ROW_ID";
    private Cursor report;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// custom view stuff
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
		
		setContentView(R.layout.activity_scstream);
		
		// custom view stuff
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title); 
		
		// DB
		myDbHelper = new DBHelper(this);
		myDbHelper.open();
		
		// custom view stuff
		header = (TextView) findViewById(R.id.header_text); 
		header.setText("Stream Data"); 
		
		// find elements
		upWidth = (EditText) findViewById(R.id.upWidth);
		downWidth = (EditText) findViewById(R.id.downWidth);
		upDepth = (EditText) findViewById(R.id.upDepth);
		downDepth = (EditText) findViewById(R.id.downDepth);
		leftAveCurrent = (Spinner) findViewById(R.id.leftAveCurrent);
		rightAveCurrent = (Spinner) findViewById(R.id.rightAveCurrent);
		leftSubstrate = (Spinner) findViewById(R.id.leftSubstrate);
		rightSubstrate = (Spinner) findViewById(R.id.rightSubstrate);
		streamRadioGroup = (RadioGroup) findViewById(R.id.streamRadioGroup);
		streamComments = (EditText) findViewById(R.id.streamComments);
		
	    // DB
	    rowID = this.getIntent().getLongExtra(this.ROW_ID, -1);
	    populateFields();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.scstream, menu);
		return true;
	}
	
	// DB
    private void populateFields() {
    	report = myDbHelper.fetchReport(rowID, "Stream_Desc");
    	// if there is data in the data base then load it into the ui elements
        if (report.getCount() == 1) {
        	upWidth.setText(Integer.toString(report.getInt(report.getColumnIndexOrThrow("upstream_width"))));
        	upDepth.setText(Integer.toString(report.getInt(report.getColumnIndexOrThrow("upstream_depth"))));
        	downWidth.setText(Integer.toString(report.getInt(report.getColumnIndexOrThrow("downstream_width"))));
        	downDepth.setText(Integer.toString(report.getInt(report.getColumnIndexOrThrow("downstream_depth"))));
        	leftAveCurrent.setSelection(getSpinnerIndex(leftAveCurrent, report.getString(report.getColumnIndexOrThrow("upstream_current"))));
        	rightAveCurrent.setSelection(getSpinnerIndex(rightAveCurrent, report.getString(report.getColumnIndexOrThrow("downstream_current"))));
        	leftSubstrate.setSelection(getSpinnerIndex(leftSubstrate, report.getString(report.getColumnIndexOrThrow("upstream_substrate"))));
        	rightSubstrate.setSelection(getSpinnerIndex(rightSubstrate, report.getString(report.getColumnIndexOrThrow("downstream_substrate"))));
        	if(report.getString(report.getColumnIndexOrThrow("adj_wetlands")).equals("Yes")){
        		streamRadioGroup.check(R.id.yesWetlands);
        	}
        	else{
        		streamRadioGroup.check(R.id.noWetlands);
        	}
        	streamComments.setText(report.getString(report.getColumnIndexOrThrow("comments")));
        }
    }
    
    // DB
    private Long saveStream(){
    	
    	String stream;
    	// radio group wetlands stuff
    	if(streamRadioGroup.getCheckedRadioButtonId() == R.id.yesWetlands){
    		stream = "Yes";
    	}
    	else{
    		stream = "No";
    	}
    	int uw;
    	int ud;
    	int dw;
    	int dd;
    	// up and down stream withds and depths set up
    	if(upWidth.getText().toString().length() == 0){
    		uw = 0;
    	}
    	else{
    		uw = Integer.parseInt(upWidth.getText().toString());
    	}
    	if(upDepth.getText().toString().length() == 0){
    		ud = 0;
    	}
    	else{
    		ud = Integer.parseInt(upDepth.getText().toString());
    	}
    	if(downWidth.getText().toString().length() == 0){
    		dw = 0;
    	}
    	else{
    		dw = Integer.parseInt(downWidth.getText().toString());
    	}
    	if(downDepth.getText().toString().length() == 0){
    		dd = 0;
    	}
    	else{
    		dd = Integer.parseInt(downDepth.getText().toString());
    	}
    	
    	// if there is no data in the report then create it all
		if(report.getCount() == 0){
			myDbHelper.createStream(rowID,
					uw,
					ud,
					leftAveCurrent.getSelectedItem().toString(),
					dw,
					dd,
					rightAveCurrent.getSelectedItem().toString(),
					leftSubstrate.getSelectedItem().toString(),
					rightSubstrate.getSelectedItem().toString(),
					stream,
					streamComments.getText().toString());
		}
		// there is data in the report so update it
		else{
			myDbHelper.updateStream(rowID,
					uw,
					ud,
					leftAveCurrent.getSelectedItem().toString(),
					dw,
					dd,
					rightAveCurrent.getSelectedItem().toString(),
					leftSubstrate.getSelectedItem().toString(),
					rightSubstrate.getSelectedItem().toString(),
					stream,
					streamComments.getText().toString());
		} 
		return rowID;
    }  
	
	/** Called when the user clicks the Continue button */
	public void cont(View view) {
		rowID = saveStream();
		myDbHelper.close();
	    
		this.finish();
	}
	
	public void cancel(View view) {
		this.finish();
	}
	
	// returns the index of the str in the spinner
	private int getSpinnerIndex(Spinner spin, String str) 
    {
    	Adapter adapter = spin.getAdapter();
    	for(int i=0; i < adapter.getCount(); i++)
    	{
    		if(adapter.getItem(i).toString().equals(str))			
    			return i;
    	}
    	return -1;
    }
	
	@Override
    public void onBackPressed() {
        super.onBackPressed();   
        myDbHelper.close();
        this.finish();
    }
	
	// setup button for help which is the question mark in the top rights
	public void tutorial(View view){
		Intent intent = new Intent(this, HTMLView.class);
		intent.putExtra(HTMLView.PATH, "file:///android_asset/stream.html");
		this.startActivity(intent);
	}

}
